package org.yoyo.game;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.TiledLayer;

import org.yoyo.core.MainLayerManager;

public class StartCanvas extends AbstractGameCanvas implements Runnable{

	private static final String imageFile = "/envir/back-tiles2.PNG";
	private static final int[][] map =new int[][]{
		{2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2},
		{2,2,2,2,2,2,2,2,2,2,3,2,2,2,0,2,2,2,2,2},
		{2,2,4,2,2,2,2,2,2,2,3,2,2,2,0,2,2,1,2,2},
		{2,2,2,2,2,4,2,2,2,2,3,3,2,2,0,2,2,2,2,2},
		{2,2,4,2,2,2,2,2,2,2,2,3,2,2,0,2,2,2,1,2},
		{2,2,4,2,2,2,2,4,2,2,2,3,2,2,0,2,2,2,2,2},
		{2,2,4,2,2,2,2,2,2,2,2,3,2,2,0,2,2,2,2,2},
		{4,4,2,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2},
		{2,2,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
		{2,1,2,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2},
		{2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2},
		{2,2,2,1,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2},
		{2,2,2,2,2,1,2,1,2,2,2,2,2,2,0,2,2,1,2,2},
		{2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,2,1,2,2,2},
		{2,2,2,1,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2},
		{2,2,2,2,2,1,2,1,2,2,2,1,2,2,0,2,2,1,2,2},
		{2,2,2,2,2,2,2,2,2,1,2,2,2,2,0,2,2,2,2,2},
		{2,1,2,1,2,1,2,2,2,2,2,2,2,2,0,2,2,2,2,2},
		{2,2,1,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2},
		{2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,2,2,2,2,2}
	};
	
	
	private TiledLayer backGround;
	private GamerSprite gamer;
	
	private static final int step = 5;
	
	protected StartCanvas(boolean suppressKeyEvents, boolean reuseable,
			int prilevel) throws IOException {
		super(suppressKeyEvents, reuseable, prilevel);
		init();
	}

	private void init() throws IOException{
		System.out.println("[StartCanvas] [init] [in]");
		
		backGround = MainLayerManager.getTiledLayer(imageFile, 20, 20, 10, 10);
		for(int i=0;i<map.length;i++){
			for(int j=0;j<map[0].length;j++)
				backGround.setCell(i, j, map[i][j]+1);
		}
		
		System.out.println("[StartCanvas] [init] [backGround:"+backGround+"] [finish]");
		
		gamer = GamerSprite.getInstance();
		System.out.println("[StartCanvas] [init] [gamer:"+gamer+"] [finish]");
		
		
	}
	
	public void doShow() {
		Graphics g = getGraphics();
		
		backGround.paint(g);
		gamer.paint(g);
		
		flushGraphics();
		
		isAviable=true;
		new Thread(this).start();
	}

	private static StartCanvas instance = null;
	public static StartCanvas getInstance() throws IOException{
		if(null == instance)
			instance = new StartCanvas(false,false,0);
		return instance;
	}

	
	private void handleRepaint(){
		
		int width = getWidth();
		int height = getHeight();
		
		int currentX = gamer.getX();
		int currentY = gamer.getY();
		
		int keyState = getKeyStates();
		
		switch (keyState) {
			case GameCanvas.UP_PRESSED:
				gamer.setPosition( currentX , currentY-step < 0 ? 0: currentY-step);
				gamer.setOrdinal(GamerSprite.ORDINAL_UP);
				break;
			case GameCanvas.RIGHT_PRESSED:
				gamer.setPosition( currentX + step >width-gamer.getWidth() ? width-gamer.getWidth(): currentX+step 
						, currentY);				
				gamer.setOrdinal(GamerSprite.ORDINAL_RIGHT);
				break;
			case GameCanvas.DOWN_PRESSED:
				gamer.setPosition( currentX 
						, currentY+step > height - gamer.getHeight() ? height- gamer.getHeight(): currentY+step);				
				gamer.setOrdinal(GamerSprite.ORDINAL_DOWN);
				break;
			case GameCanvas.LEFT_PRESSED:
				gamer.setPosition( currentX - step < 0 ? 0 : currentX - step , currentY);
				gamer.setOrdinal(GamerSprite.ORDINAL_LEFT);
				break;
	
			default:
				break;
		}
		
		System.out.println( "currentPosition:" +gamer.getX() + ":" + gamer.getY());
		
		backGround.paint(getGraphics());
		gamer.paint(getGraphics());
		flushGraphics();
	}
	
	public void run() {
		while(isShown() && isAviable){
			
			handleRepaint();
			
			try {
				Thread.currentThread().sleep(waitTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
